package com.tengju.bff.interfaces.manage.crm.brand;

import com.tengju.bff.interfaces.manage.crm.brand.facade.BrandAnalysisRankFacade;
import com.tengju.bff.interfaces.manage.crm.brand.vo.BrandSaleRankForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.export.BrandSaleRankExportQuery;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import com.tengju.motivation.domain.model.goods.Goods;
import com.tengju.motivation.domain.model.goods.GoodsRepository;
import com.tengju.motivation.domain.model.goods.GoodsType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Validated
@RestController
@Api(tags = "品牌分析")
@RequestMapping(value = "manage/crm/brand")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class BrandAnalysisController {

    private final BrandAnalysisRankFacade brandAnalysisRankFacade;
    private final ExportService exportService;
    private final GoodsRepository goodsRepository;


    @ApiOperation(value = "品牌搜索", notes = "品牌搜索")
    @GetMapping(value = "/search")
    public ApiResponse<List<KVVO<String, String>>> saleRank(
            @NotNull(message = "商品类型不能为空")
            @ApiParam("商品类型  品牌、商品(套组)、礼包")
            @RequestParam("goodType") String goodsType,
            @ApiParam("名字搜索关键词")
            @RequestParam("name") String name) {
        Map<String, Goods> goods;
        if (GoodsType.SPU.getDesc().equalsIgnoreCase(goodsType)) {
            goods = goodsRepository.findSpus(name);
        } else {
            goods = goodsRepository.findBrands(name);
        }
        List<KVVO<String, String>> list = goods == null ? null : goods.values().stream()
                .map(a -> KVVO.of(a.getName(), a.getId()))
                .collect(Collectors.toList());
        return ApiResponse.newSuccess(list);
    }

    @ApiOperation(value = "品牌销售榜", notes = "品牌销售榜")
    @RequestMapping(value = "/saleRank", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> saleRank(@Valid BrandSaleRankForm brandSaleRankForm) {
        log.info("brandSaleRankForm : {}", brandSaleRankForm);
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        List<UserIdCode> dataGenSecIdCodes = servletContext.getDataGenSecIdCodes();
        return ApiResponse.newSuccess(brandAnalysisRankFacade.brandSaleRank(brandSaleRankForm, dataGenSecIdCodes));
    }

    @ApiOperation(value = "下载品牌销售榜table", notes = "下载品牌销售榜table")
    @RequestMapping(value = "download", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<Object> asyncExport(@Valid @RequestBody BrandSaleRankForm brandSaleRankForm) {
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        List<UserIdCode> dataGenSecIdCodes = ServletContextUtil.getServletContext().getDataGenSecIdCodes();
        BrandSaleRankExportQuery exportQuery = new BrandSaleRankExportQuery();
        exportQuery.setDataGenSecIdCodes(dataGenSecIdCodes);
        exportQuery.setPageIndex(1);
        exportQuery.setPageSize(50000);
        exportQuery.setDateType(brandSaleRankForm.getDateType());
        exportQuery.setCrowdPerspective(brandSaleRankForm.getCrowdPerspective());
        exportQuery.setBelongSecIdCode(brandSaleRankForm.getBelongSecIdCode());
        exportQuery.setBelongEcIdCode(brandSaleRankForm.getBelongEcIdCode());
        exportQuery.setBrandId(brandSaleRankForm.getBrandId());
        exportQuery.setIdCode(brandSaleRankForm.getIdCode());
        exportQuery.setClubId(brandSaleRankForm.getClubId());
        exportQuery.setSaleMoneyFrom(brandSaleRankForm.getSaleMoneyFrom());
        exportQuery.setSaleMoneyTo(brandSaleRankForm.getSaleMoneyTo());
        exportQuery.setDirect(brandSaleRankForm.isDirect());
        exportQuery.setOrderColumn(brandSaleRankForm.getOrderColumn());
        exportQuery.setOrderType(brandSaleRankForm.getOrderType());
        exportService.exportData("品牌销售榜列表", ExportFileType.XLSX,
                "brandSaleRankExportDataSourceRepository",
                ExportDataType.TABLE, exportQuery,
                staffId, realName);
        return ApiResponse.newSuccess();
    }

    @ApiOperation(value = "销售额汇总", notes = "销售额汇总")
    @RequestMapping(value = "/totalSaleRank", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<String> totalSaleRank(@Valid BrandSaleRankForm brandSaleRankForm) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        List<UserIdCode> dataGenSecIdCodes = servletContext.getDataGenSecIdCodes();
        return ApiResponse.newSuccess(brandAnalysisRankFacade.totalBrandSale(brandSaleRankForm, dataGenSecIdCodes));
    }


}
